package com.xkstream.client.ui.live;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xkstream.client.R;
import com.xkstream.client.application.Application;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.entity.Admin;
import com.xkstream.client.entity.User;
import com.xkstream.client.presenter.AnchorInfoContract;
import com.xkstream.client.presenter.AnchorInfoPresenter;
import com.xkstream.client.utils.AsyncImage;
import com.xkstream.client.utils.PreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/3/21.
 */

public class EditAnchorInfoActivity extends BaseActivity<AnchorInfoContract.Presenter> implements AnchorInfoContract.View, View.OnClickListener {
    @BindView(R.id.llAnchorInfo)
    LinearLayout llAnchorInfo;
    @BindView(R.id.ivHeader)
    ImageView ivHeader;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvRoomNum)
    TextView tvRoomNum;
    @BindView(R.id.tvRoomNotice)
    TextView tvRoomNotice;
    @BindView(R.id.llAdmin)
    LinearLayout llAdmin;
    @BindView(R.id.llContent)
    LinearLayout llContent;
    @BindView(R.id.tvRoomName)
    TextView tvRoomName;
    @BindView(R.id.llAdminList)
    LinearLayout llAdminList;
    @BindView(R.id.ivClose)
    ImageView ivClose;

    private Context context;


    private List<Admin> list = new ArrayList<>();

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_editanchorinfo);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        context = this;
        presenter = new AnchorInfoPresenter(this);
        llAnchorInfo.getBackground().setAlpha(40);
        llAdminList.getBackground().setAlpha(40);
        prepareViews();
        loadAdminList();
        ivClose.setOnClickListener(this);
    }

    private void prepareViews() {
        User user = Application.getUser();
        AsyncImage.loadPhoto(context, user.getExt_info().getHead_icon(), ivHeader);
        tvName.setText(user.getExt_info().getNickname());
        tvRoomNum.setText(user.getAnchor().getRoom_id_pk() + "");
        String anchorTitle = PreferenceUtils.getString(PreferenceUtils.ANCHOR_TITLE + user.getUser_code());
        if (!TextUtils.isEmpty(anchorTitle)) {
            tvRoomName.setText(anchorTitle);
        }
        String anchorDes = PreferenceUtils.getString(PreferenceUtils.ANCHOR_DES + user.getUser_code());
        if (!TextUtils.isEmpty(anchorDes)) {
            tvRoomNotice.setText(anchorDes);
        }
    }

    private void loadAdminList() {
        User user = Application.getUser();
        presenter.getAdminList(user.getUser_code());
    }

    private void addAdminViews(List<Admin> list) {
        for (Admin admin : list) {
            addAdminView(admin);
        }
    }

    private void addAdminView(Admin admin) {
        View view = View.inflate(context, R.layout.layout_admin_item, null);
        ImageView ivHeader = (ImageView) view.findViewById(R.id.ivHeader);
        TextView tvAdminName = (TextView) view.findViewById(R.id.tvAdminName);
        AsyncImage.loadPhoto(context, admin.getHead_icon(), ivHeader);
        tvAdminName.setText(admin.getNickname());
        llAdmin.addView(view);
        view.setTag(admin);
        view.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivClose:
                finish();
                break;
            default:
                Admin admin = (Admin) v.getTag();
                sureRemoveAdminDialog(admin);
                break;
        }

    }

    private void removeAdmin(String userCode) {
        int position = 0;
        for (int i = 0; i < list.size(); i++) {
            Admin admin = list.get(i);
            if (admin.getUser_code_fk().equals(userCode)) {
                position = i;
                break;
            }
        }
        llAdmin.removeViewAt(position);
        list.remove(position);
    }

    private void sureRemoveAdminDialog(final Admin admin) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(R.string.alert_remove_admin);
        builder.setTitle(R.string.warn);
        builder.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (admin != null) {
                    User user = Application.getUser();
                    String userCode = admin.getUser_code_fk();
                    presenter.removeAdmin(user.getUser_code(), user.getAnchor().getNeteaseChatRoomId(),
                            userCode);
                }
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.create().show();
    }

    @Override
    public void setPresenter(AnchorInfoContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showAdminList(List<Admin> data) {
        list.clear();
        list.addAll(data);
        addAdminViews(list);
    }

    @Override
    public void showRemoveAdminResult(int code, String userCode) {
        if (code == 1) {
            removeAdmin(userCode);
            showToast(R.string.remove_admin_success);
        } else {
            showToast(R.string.remove_admin_failed);
        }
    }
}
